package org.xshop.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.model.TMemberAddress;
import org.xshop.model.TParam;
import org.xshop.service.ITMemberAddressService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.support.Assert;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 收货地址 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2017-04-21
 */
@Controller
@RequestMapping("/app/member/")
@Api(tags = "MemberAddress", value = "个人中心-地址管理接口", description = "APP-个人中心-地址管理接口")
public class TMemberAddressController extends AppBaseController<TMemberAddress, ITMemberAddressService> {
    @ApiOperation(value = "个人收货地址列表", produces = MediaType.APPLICATION_JSON_VALUE, notes = "获取个人收货地址列表", response = TMemberAddress.class)
    @RequestMapping(value = "myAddressList.api", method = { RequestMethod.GET, RequestMethod.POST })
    public Object query(HttpServletRequest request, TParam param) {
        param = WebUtil.getParameter(request, TParam.class);
        Long id = getCurrUser(request);
        if (DataUtil.isNotEmpty(id)) {
            param.setId(id);
        }
        Assert.notNull(param.getId(), "ID");
        Map<String, Object> params = InstanceUtil.newHashMap();
        params.put("memberId", param.getId());
        params.put("orderBy", "is_default desc, id_");
        return super.query(params);
    }

    @Override
    @PostMapping("updateAddress.api")
    @ApiOperation(value = "添加个人收货地址", notes = "添加收货地址", produces = MediaType.APPLICATION_JSON_VALUE, response = TMemberAddress.class)
    public Object update(HttpServletRequest request, TMemberAddress param) {
        param = WebUtil.getParameter(request, TMemberAddress.class);
        Long id = getCurrUser(request);
        if (DataUtil.isNotEmpty(id)) {
            param.setMemberId(id);
        }
        Object result = service.updateAddress(param);
        return setSuccessModelMap(result);
    }

    @PostMapping("delAddress.api")
    @ApiOperation(value = "删除个人收货地址", notes = "删除收货地址", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object delete(HttpServletRequest request, TParam param) {
        param = WebUtil.getParameter(request, TParam.class);
        Assert.notNull(param.getId(), "ID");
        TMemberAddress params = new TMemberAddress();
        Long id = getCurrUser(request);
        if (DataUtil.isNotEmpty(id)) {
            params.setMemberId(id);
        }
        params.setId(param.getId());
        service.deleteByEntity(params);
        return setSuccessModelMap();
    }
}
